package ysq.xyg.mob.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.mob.dao.MobBannerDao;
import ysq.xyg.mob.model.po.MobBanner;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.mob.model.vo.MobBannerVo;
import ysq.xyg.mob.service.MobBannerService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 移动端模块-轮播图service实现
 *
 * @author shengQiang yu
 * @date 2019-10-24 17:09:42
 */
@Service("mobBannerService")
public class MobBannerServiceImpl extends BaseServiceImpl<MobBanner> implements MobBannerService {

    @Resource
    private MobBannerDao mobBannerDao;

    private MobBanner mobBanner;

    @Autowired
    public void setBaseDao(MobBannerDao mobBannerDao) {
        this.baseDao = mobBannerDao;
    }


    /**
     * 分页查询
     *
     * @param mobBannerVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:42
     */
    @Override
    public ReturnEntity page(MobBannerVo mobBannerVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<MobBanner> list = acceptList(mobBannerVo);
        PageInfo<MobBanner> page=new PageInfo<MobBanner>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param mobBannerVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:42
     */
    @Override
    public ReturnEntity addOrUpdate(MobBannerVo mobBannerVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(mobBannerVo.getId())){
            mobBanner =new MobBanner(currentUser);
            isUpate=false;
        }else{
            mobBanner =selectByPrimaryKey(mobBannerVo.getId());
            if(ObjectUtil.isNull(mobBanner)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            mobBanner.setUpdateTime(new Date());
            mobBanner.setUpdateUser(currentUser.getId());
        }
        mobBanner.setMobBannerCode(mobBannerVo.getMobBannerCode());
        mobBanner.setImage(mobBannerVo.getImage());
        mobBanner.setUrl(mobBannerVo.getUrl());
        mobBanner.setDescription(mobBannerVo.getDescription());
        mobBanner.setSort(mobBannerVo.getSort());
        mobBanner.setShowType(mobBannerVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(mobBanner);
        }else{
            num=insert(mobBanner);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 17:09:42
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        MobBanner MobBanner =selectByPrimaryKey(id);
        if(MobBanner !=null){
            mobBanner.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            mobBanner.setUpdateTime(new Date());
            mobBanner.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(MobBanner);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param mobBannerVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-24 17:09:42
     */
    @Override
    public List<MobBanner> acceptList(MobBannerVo mobBannerVo){
        Example example = new Example(MobBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(mobBannerVo.getRemoveTag())){
            criteria.andEqualTo("removeTag", mobBannerVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(mobBannerVo.getShowType())){
            criteria.andEqualTo("showType",mobBannerVo.getShowType());
        }
        example.setOrderByClause(" update_time desc");
        return mobBannerDao.selectByExample(example);
    }
}
